/**
 * @(#) CertificateProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 제증명신청 Proxy - Proxy
 * 				제증명서 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.certificate.model
{
	import com.skt.common_hr.certificate.CertificateFacade;
	import com.skt.common_hr.certificate.model.vo.CertificatePrintVO;
	import com.skt.common_hr.certificate.model.vo.CertificateSaveVO;
	import com.skt.common_hr.certificate.model.vo.CertificateVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class CertificateProxy extends CommonProxy
	{
		public static const NAME:String = "CertificateProxy";
		
		public var certiList:ArrayCollection;
		public var certiPrintList:ArrayCollection;
		public var certiPrintListNos:ArrayCollection;
		public var certiPrintType:String;
		
		public var certiPrintWonchunUrl:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function CertificateProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_PROC_TYPE"] = "S"; 			//S-조회, I-신규, U-수정, D-삭제
			loadData("Z_HR_0020", params, loadResult, loadFault); 
		}
		
		
		public function insert(pernr:String, rec:CertificateSaveVO):void
		{
			
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_PROC_TYPE"] = "I"; 			//S-조회, I-신규, U-수정, D-삭제
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["I_TABLE"] = table;

			saveData("Z_HR_0020", params, tables, insertResult, insertFault); 
		}

		public function update(pernr:String, rec:CertificateSaveVO):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_PROC_TYPE"] = "U"; 			//S-조회, I-신규, U-수정, D-삭제
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["I_TABLE"] = table;

			saveData("Z_HR_0020", params, tables, updateResult, updateFault); 
		}

		public function del(pernr:String, rec:CertificateSaveVO):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_PROC_TYPE"] = "D"; 			//S-조회, I-신규, U-수정, D-삭제
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["I_TABLE"] = table;

			saveData("Z_HR_0020", params, tables, delResult, delFault); 
		}
		
		public function loadPrint(rec:CertificateVO):void
		{
			var params:Object = new Object();
			var obj:Object = new Object();
			params["I_ACTIO"] = "PRT";
			
			var tables:Object = new Object();
			var table:Array = new Array();
			
			obj["BUKRS"]				= rec.BUKRS;
			obj["SEQNR"]				= rec.SEQNR;
			obj["PERNR"]				= rec.PERNR;
			obj["DTYPE"]				= rec.DTYPE;
			obj["WHYNEED"]				= rec.WHYNEED;
			
			table.push(obj);
			tables["T_IN"] = table;		
			
			saveData("Z_HR_IN09", params, tables, printResult, printFault); 
		}

		// 원천 징수 영수증 출력  2014
		public function loadPrintWonchun(rec:CertificateVO, sRegno:String):void
		{
			var params:Object = new Object();
			var obj:Object = new Object();
			params["I_SEQNR"] = rec.SEQNR;
			params["I_REGNO"] = sRegno;
			
			var tables:Object = new Object();
			var table:Array = new Array();
			
			table.push(obj);				
			saveData("Z_HR_C001", params, tables, printWonchunResult, printFault); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				certiList = parseCertiList(event.result.O_TABLE);
				facade.sendNotification(CertificateFacade.LOAD_CERTIFICATE_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.LOAD_CERTIFICATE_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(CertificateFacade.LOAD_CERTIFICATE_LIST_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(CertificateFacade.INSERT_CERTIFICATE_COMPLETE, this);
			}
			else {
				trace( "failed to insert certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.INSERT_CERTIFICATE_FAILED, this);				
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(CertificateFacade.INSERT_CERTIFICATE_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(CertificateFacade.UPDATE_CERTIFICATE_COMPLETE, this);
			}
			else {
				trace( "failed to update certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.UPDATE_CERTIFICATE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(CertificateFacade.UPDATE_CERTIFICATE_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(CertificateFacade.DELETE_CERTIFICATE_COMPLETE, this);
			}
			else {
				trace( "failed to delete certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.DELETE_CERTIFICATE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function delFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(CertificateFacade.DELETE_CERTIFICATE_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function printResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				certiPrintType = event.result.T_IN[0].DTYPE;
				certiPrintList = parseCertiPrintList(event.result.T_OUT);
				
				facade.sendNotification(CertificateFacade.PRINT_CERTIFICATE_COMPLETE, this);
			}
			else {
				trace( "failed to print popup certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.PRINT_CERTIFICATE_FAILED, this);
			}
		}

		public function printWonchunResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				certiPrintWonchunUrl = event.result.O_PATH;
				facade.sendNotification(CertificateFacade.PRINT_CERTIFICATE_WONCHUN_COMPLETE, this);
			}
			else {
				trace( "failed to print popup certi: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CertificateFacade.PRINT_CERTIFICATE_FAILED, this);
			}
		}		
		
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function printFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(CertificateFacade.DELETE_CERTIFICATE_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명 신청 현황 ArrayCollection
		 */
		private function parseCertiList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:CertificateVO = new CertificateVO();
				vo.SEQNR		= rec["SEQNR"];
				vo.PERNR 		= StringUtil.trimFront(rec["PERNR"], "0");
				vo.IDATE		= DateUtil.stringToDateTime(rec["IDATE"]);
				vo.STATE 		= rec["STATE"];
				vo.STATE_TXT	= rec["STATE_TXT"];
				vo.DTYPE		= rec["DTYPE"];
				vo.DTYPE_TXT 	= rec["DTYPE_TXT"];
				vo.COPY			= rec["COPY"];
				vo.WHYNEED 		= rec["WHYNEED"];
				vo.WHY_TXT 		= rec["WHY_TXT"];
				vo.HOWSEND 		= rec["HOWSEND"];
				vo.HOW_TXT 		= rec["HOW_TXT"];
				vo.ACQ_TXT		= rec["ACQ_TXT"];
				vo.PFLAG		= rec["PFLAG"];
				vo.BUKRS		= rec["BUKRS"];
				vo.AEDTM 		= DateUtil.stringToDateTime(rec["AEDTM"]);
				
				vo.DOCNO		= rec["DOCNO"];
				vo.ENAME		= rec["ENAME"];
				vo.JWTXT		= rec["JWTXT"];
				vo.ORGTX		= rec["ORGTX"];
				vo.PNAME		= rec["PNAME"];
				vo.PERIO		= rec["PERIO"];
				vo.UNAME		= rec["UNAME"];
				
				vo.REGNO		= rec["REGNO"];
				vo.ADDRE		= rec["ADDRE"];
				
				vo.PDATE		= DateUtil.stringToDateTime(rec["PDATE"]); 
				
				if(Variables.company == 'NOS'){
					if(vo.STATE == '2' && vo.DTYPE == '01'){
						vo.PFLAG = 'X';
					}else {
						vo.PFLAG = '';
					}
				}
				
				vo.RYEAR		= rec["RYEAR"];
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명 신청 현황 ArrayCollection
		 */
		private function parseCertiPrintList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:CertificatePrintVO = new CertificatePrintVO();
				vo.DOCNO		= rec["DOCNO"];
				vo.ENAME 		= rec["ENAME"];
				vo.JCTXT		= rec["JCTXT"];
				vo.ORGTX		= rec["ORGTX"];
				vo.PERIO 		= rec["PERIO"];
				vo.PERNR 		= rec["PERNR"];
				vo.PNAME 		= rec["PNAME"];
				vo.REGNO		= rec["REGNO"];
				vo.UTEXT		= rec["UTEXT"];
				vo.ADDRE		= rec["ADDRE"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		public function savePrint(vo:CertificateVO, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_SEQNR"] = vo.SEQNR;
			
			var table:Object = {};

			saveData("Z_HR_C001", params, table, savePrintResult, saveFault, setFunc);
		}
 
		public function savePrintResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show(retText);
			}
		}
		
		private function saveFault(event:FaultEvent):void
		{
			Alert.show('WorkContractProxy.as listFault err: ' + retText);
		}

	}
}